package com.lill.product.web;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.product.model.ProductBaseModel;
import com.lill.product.model.ProductBoutiqueModel;
import com.lill.product.model.ProductCarModel;
import com.lill.product.service.IProductBaseService;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.constant.SystemConstant;
import com.lill.system.model.SysUserEntity;

/**
 * 产品基本信息Controller
 *
 * @author lill
 * @date 2021-08-27 09:14:43
 */
@RestController
@RequestMapping("product/base")
public class ProductBaseController {
    @Autowired
    private IProductBaseService productBaseService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	if(!user.getId().equals(SystemConstant.SUPER_ADMIN)){
    		params.put("companyId", user.getCompanyId());
    	}
        //查询列表数据
        Query query = new Query(params);
        List<ProductBaseModel> productBaseList = productBaseService.queryList(query);
        int total = productBaseService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(productBaseList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
    	ProductBaseModel productBase = productBaseService.queryObject(id);
        return R.ok().put("productBase", productBase);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	if(Arrays.asList(ids).contains(ProductBoutiqueModel.classify)
    			||Arrays.asList(ids).contains(ProductCarModel.classify)){
    		return R.failure().put("msg", "系统权限不足");
    	}
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
        int result=productBaseService.deleteLogicByMap(params);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	if(!user.getId().equals(SystemConstant.SUPER_ADMIN)){
    		params.put("companyId", user.getCompanyId());
    	}
        List<ProductBaseModel> list = productBaseService.queryList(params);
        return R.ok().put("list", list);
    }
    
}
